<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="">
<head>

    <title></title>
    <!-- jqGrid组件基础样式包-必要 -->
    <link rel="stylesheet" th:href="@{/admin/dist/js/jqgrid-5.3.0/ui.jqgrid.css}"/>
</head>
<style>
    .ui-jqgrid tr.jqgrow td {
        height: auto;
        text-align: center;
        font-size: 20px;
        padding-top: 10px;
        padding-bottom: 10px;
        vertical-align: middle;
    }

    .ui-th-column {
        padding: 20px;
    }
</style>
<header th:replace="admin/header::header-fragment">
</header>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <!-- 引入页面头header-fragment -->
    <div th:replace="admin/header::header-nav"></div>
    <!-- 引入工具栏sidebar-fragment -->
    <div th:replace="admin/sidebar::sidebar-fragment(${path})"></div>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
            </div><!-- /.container-fluid -->
        </div>
        <!-- Main content -->
        <div class="content">
            <div class="container-fluid">
                <div class="card card-primary card-outline">
                    <div class="card-header">
                        <h3 class="card-title">博客管理</h3>
                    </div> <!-- /.card-body -->
                    <div class="card-body">
                        <div class="grid-btn">
                            <button class="btn btn-success" onclick="addBlog()"><i
                                    class="fa fa-plus"></i>&nbsp;新增
                            </button>
                            <button class="btn btn-info" onclick="editBlog()"><i
                                    class="fa fa-edit"></i>&nbsp;修改
                            </button>
                            <button class="btn btn-danger" onclick="deleteBlog()"><i
                                    class="fa fa-trash-o"></i>&nbsp;删除
                            </button>&nbsp;&nbsp;
                            <input type="text" placeholder="关键字(标题/分类)" id="keyword" class="form-control col-2">&nbsp;
                            <button class="btn btn-info" onclick="search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索
                            </button>
                        </div>
                        <!-- JqGrid必要DOM,用于创建表格展示列表数据 -->
                        <table id="jqGrid" class="table-bordered"></table>
                        <!-- JqGrid必要DOM,分页信息区域 -->
                        <div id="jqGridPager"></div>
                    </div><!-- /.card-body -->
                </div>
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <!-- 引入页脚footer-fragment -->
    <div th:replace="admin/footer::footer-fragment"></div>
</div>
<script th:src="@{/admin/dist/js/jquery/jquery-3.5.1.min.js}"></script>
<script src="/admin/dist/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- AdminLTE App -->
<script src="/admin/dist/js/adminlte.min.js"></script>
<!-- jqgrid -->
<script th:src="@{/admin/dist/js/jqgrid-5.3.0/jquery.jqGrid.min.js}"></script>
<script th:src="@{/admin/dist/js/jqgrid-5.3.0/grid.locale-cn.js}"></script>
<script th:src="@{/admin/dist/plugins/sweetalert/sweetalert.min.js}"></script>
<script>
    $(function () {
        $("#jqGrid").jqGrid({
            url: '/admin/blogs/list',
            mtype: "get",
            datatype: "json",
            jsonReader: {
                root: "data.blogs",
                page: "data.currPage",
                total: "data.pageCount",
                records: "data.total"
            },
            colNames: ['id', '标题', '博客图标', '浏览量', '博客分类', '添加时间'],
            //jqgrid主要通过下面的索引信息与后台传过来的值对应
            colModel: [
                {name: 'id', index: 'id', hidden: true,},
                {name: 'title', index: 'blogTitle', align: "center",},
                {name: 'icon', index: 'blogIcon', formatter: imgFormatter},
                {name: 'views', index: 'blogViews', align: "center"},
                {name: 'catalogue_name', index: 'blogCategoryName', align: "center"},
                {
                    name: 'time',
                    index: 'time',
                    align: "center",
                    formatter: 'date',
                    formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'}
                },   //没有srcformat: 'Y-m-d H:i:s',时分秒就会使00:00:00
            ],
            height: 630,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            styleUI: 'Bootstrap',
            rowList: [5, 10, 20],
            rowNum:5,
            // caption: "Cell Edit Example",
        });
    })

    function imgFormatter(cellvalue) {
        return "<a href='" + cellvalue + "' target='_blank'> <img src='" + cellvalue + "' height='105' width='150' alt='icon'/></a>";
    }

    function addBlog() {
        window.location.href = "/admin/blogs/edit";
    }

    function editBlog() {
        let grid = $("#jqGrid");
        let s = grid.jqGrid('getGridParam', 'selarrrow');
        let rowKey = grid.getGridParam("selrow");

        if (null == rowKey) {
            swal("请选择一条记录");
            return;
        }

        // if (!rowKey) {
        //     alert("的")
        // }

        if (s.length > 1) {
            swal("只能选择一条记录");
            return;
        }

        // alert(s);
        window.location.href = "/admin/blogs/edit/" + s;
    }

    function deleteBlog() {
        let selectedRows = getSelectedRows();
        // alert(ids.length);
        if (selectedRows.length === 0) {
            swal("请先勾选要删除的博客！")
            return;
        }
        let ids = {ids: selectedRows};
        console.info(JSON.stringify(ids));

        $.ajax({
            url: "/admin/blogs/delete",
            type: "post",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(ids),
            success: function (data) {
                // alert(data);
                if (data.code === 1000) {
                    swal("Very Good!", "删除成功!", "success");
                    $("#jqGrid").trigger("reloadGrid");
                } else {
                    swal("Unfortunately！", "删除失败！", "error");
                }
            }
        });
    }

    function search() {
        let keyword = $("#keyword").val();
        // alert(keyword);
        // $("#jqGrid").setGridParam({
        //     url : '/admin/blogs/list/'+keyword,
        // }).trigger("reloadGrid");
        $("#jqGrid").jqGrid("setGridParam", {url: '/admin/blogs/list/' + keyword}).trigger("reloadGrid");
    }

    /**
     * 获取jqGrid选中的多条记录
     * @returns {*}
     */
    function getSelectedRows() {
        var grid = $("#jqGrid");
        return grid.getGridParam("selarrrow");
    }
</script>
</body>
</html>
